#!/usr/bin/env python
# coding: utf-8
import sys

sys.path.append("..")
import Lib.wxbot
import pyqrcode
import os
import thread
import time
import json
import requests
import threading
import base64
from traceback import format_exc


def run(web_input, action, bot_list):
    # action ---> http://127.0.0.1/api/*****/  其中×××对应的就是action,通过action字段来实现自定义的操作在下面主程序编写业务逻辑
    # 返回格式如下，code目前为200和500，200为正常，500为异常
    # {'code':200,'error_info':'','data':''}

    # test
    if action == 'test':
        try:
            bot_id = web_input['bot_id']
            for x in bot_list:
                if x.bot_id == bot_id:
                    head_image_name = x.bot.get_icon(x.bot.my_account['UserName'])
                    with open('temp/' + head_image_name, "rb") as f:
                       base64_data = base64.b64encode(f.read())
                    data = {
                        'contact_list': base64_data
                    }
                    return {'code': 200, 'error_info': '', 'data': data}
            return {'code': 500, 'error_info': 'bot_id not found!!', 'data': ''}
        except Exception, e:
            return {'code': 500, 'error_info': str(e), 'data': ''}